package com.budwk.app.util.poiUtils;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.dto.FireHiddenExcelParamYuHongSQDTO;
import com.budwk.app.sqfw.enums.DownLoadFileNameEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.FireHiddenExcelYuHongBO;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * 宇虹社区excel
 *
 * @author yangweimin
 * @since 2023/10/18
 */
public class YuHongMenStreetFireHiddenExcelUtils {

    /**
     * 宇虹社区消防安全excel(总共有30个排查问题，如果30个都正常，则在表格里只生成一条数据；否则，有几条隐患，就生成几条数据)
     *
     * @param excelParamDTO excel参数
     * @return excel链接
     */
    public static String createExcelSQ(FireHiddenExcelParamYuHongSQDTO excelParamDTO, String isCheckData) {
        String unitName = excelParamDTO.getCompleteUnitName();
        String userName = excelParamDTO.getCreateUserName();
        String phone = excelParamDTO.getCreateUserPhone();
        String county = excelParamDTO.getCounty();
        String community = excelParamDTO.getCommunity();
        Map<String, Object> titleMap = Maps.newHashMap();
        titleMap.put("province", excelParamDTO.getProvince());
        titleMap.put("city", excelParamDTO.getCity());
        titleMap.put("district", excelParamDTO.getDistrict());
        titleMap.put("county", excelParamDTO.getCounty());
        titleMap.put("community", excelParamDTO.getCommunity());
        if ("isTemplate-web".equals(isCheckData)) {
            titleMap.put("community", "云潭街道");
        } else {
            titleMap.put("community", excelParamDTO.getCommunity());
        }
        int fileIndex = excelParamDTO.getFileIndex();
        List<Sqfw_formSummary> formSummaryList = excelParamDTO.getFormSummaryList();
        // zhangp 2023-11-27 下载模板放行
        /*if (formSummaryList == null || formSummaryList.size() == 0) {
            return null;
        }*/

        // 风险、隐患排查整治台账
        if (fileIndex == 1) {
            titleMap.put("createBy", userName);
            titleMap.put("downloadDate", DateUtil.format(new Date(), "yyyy年MM月dd日"));

            List<Map<String, Object>> dataList = Lists.newArrayList();

            int num = 0;
            for (Sqfw_formSummary formSummary : formSummaryList) {
                int total = 0;
                JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(formSummary));
                //A21-A45的问题
                for (int i = 21; i < 46; i++) {
                    String key = "a" + i;
                    JSONArray jsonArray = jsonObject.getJSONArray(key);
                    String array_1 = jsonArray.getString(0);
                    String array_2 = jsonArray.getString(1);
                    String array_3 = jsonArray.getString(2);
                    String array_4 = jsonArray.getString(3);
                    String array_5 = jsonArray.getString(4);
                    String array_6 = jsonArray.getString(5);
                    String change = i < 45 ? getChange(formSummaryList, formSummary, key) :
                            getHiddenChange(formSummaryList, formSummary, key);
                    if ("未录入黔小消".contains(array_5)) {
                        continue;
                    }
                    if (!"是/否/无".contains(array_1) && !"已整改".equals(array_1)) {

                        //zhangp 2023-12-08 检查数据时只要有数据就返回
                        if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                            return "true";
                        }

                        total++;
                        num++;
                        Map<String, Object> rollDataMap = Maps.newHashMap();
                        rollDataMap.put("num", num);
                        rollDataMap.put("fillDate", DateUtil.format(formSummary.getFillDate(), "yyyy年MM月dd日"));
                        if ("居民住宅".equals(formSummary.getA5())) {
                            rollDataMap.put("a1_a2", formSummary.getA2());
                        } else {
                            rollDataMap.put("a1_a2", formSummary.getA1());
                        }
                        rollDataMap.put("hiddenProblem_3", "已整改".equals(array_1) ? "暂无" : array_3);
                        rollDataMap.put("a5", formSummary.getA5());
                        rollDataMap.put("hiddenProblem_4", array_4);
                        rollDataMap.put("hiddenProblem_5", "已整改".equals(array_1) ? "暂未发现隐患" : array_5);
                        rollDataMap.put("hiddenProblem_2", array_2);
                        String a7 = formSummary.getA7();
                        String day = "立即";
                        if (!StrUtil.isBlank(a7)) {
                            Date date = DateUtil.parse(a7, "yyyy年MM月dd日");
                            day = getDay(formSummary.getFillDate(), date);
                        }
                        rollDataMap.put("a7", array_1.contains("现场") ? "立即" : day);
                        rollDataMap.put("a15", StringUtils.join(JSON.parseArray(formSummary.getA15()), ","));
                        rollDataMap.put("a1_a2_1", rollDataMap.get("a1_a2"));
                        rollDataMap.put("a3", formSummary.getA3());
                        rollDataMap.put("a19", formSummary.getA19());
                        rollDataMap.put("hiddenProblem_6", array_6);
                        rollDataMap.put("change", array_1.contains("现场") ? "已整改" : change);
                        rollDataMap.put("remark", null);
                        dataList.add(rollDataMap);
                    }
                }

                //A8-A12、A14的问题
                for (int i = 8; i < 15; i++) {
                    if (i != 13) {
                        String key = "a" + i;
                        JSONArray jsonArray = jsonObject.getJSONArray(key);
                        String array_1 = jsonArray.getString(0);
                        String array_2 = jsonArray.getString(1);
                        String array_3 = jsonArray.getString(2);
                        String array_4 = jsonArray.getString(3);
                        String array_5 = jsonArray.getString(4);
                        String array_6 = jsonArray.getString(5);
                        String change = i < 14 ? getChange(formSummaryList, formSummary, key) :
                                getHiddenChange(formSummaryList, formSummary, key);
                        if ("未录入黔小消".contains(array_5)) {
                            continue;
                        }
                        if (!"是/否/正常/已安装并正常使用/无".contains(array_1) && !"已整改".equals(array_1)) {

                            //zhangp 2023-12-08 检查数据时只要有数据就返回
                            if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                                return "true";
                            }

                            total++;
                            num++;
                            Map<String, Object> rollDataMap = Maps.newHashMap();
                            rollDataMap.put("num", num);
                            rollDataMap.put("fillDate", DateUtil.format(formSummary.getFillDate(), "yyyy年MM月dd日"));
                            rollDataMap.put("a1_a2", formSummary.getA1());
                            rollDataMap.put("hiddenProblem_3", "已整改".equals(array_1) ? "暂无" : array_3);
                            rollDataMap.put("a5", formSummary.getA5());
                            rollDataMap.put("hiddenProblem_4", array_4);
                            rollDataMap.put("hiddenProblem_5", "已整改".equals(array_1) ? "暂未发现隐患" : array_5);
                            rollDataMap.put("hiddenProblem_2", array_2);
                            String a7 = formSummary.getA7();
                            String day = "立即";
                            if (!StrUtil.isBlank(a7)) {
                                Date date = DateUtil.parse(a7, "yyyy年MM月dd日");
                                day = getDay(formSummary.getFillDate(), date);
                            }
                            rollDataMap.put("a7", array_1.contains("现场") ? "立即" : day);
                            rollDataMap.put("a15", StringUtils.join(JSON.parseArray(formSummary.getA15()), ","));
                            rollDataMap.put("a1_a2_1", rollDataMap.get("a1_a2"));
                            rollDataMap.put("a3", formSummary.getA3());
                            rollDataMap.put("a19", formSummary.getA19());
                            rollDataMap.put("hiddenProblem_6", array_6);
                            rollDataMap.put("change", array_1.contains("现场") ? "已整改" : change);
                            rollDataMap.put("remark", null);
                            dataList.add(rollDataMap);
                        }
                    }
                }

//                if (total == 0) {
//                    num++;
//                    Map<String, Object> rollDataMap = Maps.newHashMap();
//                    rollDataMap.put("num", num);
//                    rollDataMap.put("fillDate", DateUtil.format(formSummary.getFillDate(), "yyyy年MM月dd日"));
//                    if ("居民住宅".equals(formSummary.getA5())) {
//                        rollDataMap.put("a1_a2", formSummary.getA2());
//                    } else {
//                        rollDataMap.put("a1_a2", formSummary.getA1());
//                    }
//                    rollDataMap.put("hiddenProblem_3", "暂无");
//                    rollDataMap.put("a5", formSummary.getA5());
//                    rollDataMap.put("hiddenProblem_4", null);
//                    rollDataMap.put("hiddenProblem_5", "暂未发现隐患");
//                    rollDataMap.put("hiddenProblem_2", null);
//                    rollDataMap.put("a7", null);
//                    rollDataMap.put("a15", null);
//                    rollDataMap.put("a1_a2_1", null);
//                    rollDataMap.put("a3", null);
//                    rollDataMap.put("a19", formSummary.getA19());
//                    rollDataMap.put("hiddenProblem_6", null);
//                    rollDataMap.put("change", null);
//                    rollDataMap.put("remark", null);
//                    dataList.add(rollDataMap);
//                }
            }

            //zhangp  2023-12-04 检测数据时使用
            if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                if (dataList.isEmpty()) {
                    return "false";
                }
            }
            String templatePath = CommonFormOfficeUtils.getTemplatePathByOS("yuHong/risksAndHiddenDangers.xlsx");
            //String fileName = "风险、隐患排查整治台账";
            //zhangp 2023-12-13 文件名统一
            String fileName = DownLoadFileNameEnum.YT_RISK_HIDDEN_INSPECTION.getName();
            if (StringUtils.isNotBlank(excelParamDTO.getFileNameSuffix()))
                fileName += excelParamDTO.getFileNameSuffix();

            ExcelFileCreateParamBO excelFileCreateParam = new ExcelFileCreateParamBO();
            excelFileCreateParam.setTemplatePath(templatePath);
            excelFileCreateParam.setSheetParam(titleMap);
            excelFileCreateParam.setDataList(dataList);
            excelFileCreateParam.setClazz(String.class);
            excelFileCreateParam.setFileName(fileName);
            excelFileCreateParam.setReturnWebUrlFlag(excelParamDTO.getReturnWebUrlFlag());

            return CommonFormOfficeUtils.createExcelAndUpload(excelFileCreateParam);
        }

        // 消防安全巡查台账
        if (fileIndex == 2) {
            titleMap.put("createBy", userName);
            if ("isTemplate-web".equals(isCheckData)) {
                titleMap.put("county_community", "云潭街道");
            } else {
                titleMap.put("county_community", county + "-" + (StrUtil.isBlank(community) ? "" : community));
            }
            titleMap.put("downloadDate", DateUtil.format(new Date(), "yyyy年MM月dd日"));

            List<Map<String, Object>> dataList = Lists.newArrayList();

            int num = 0;
            for (Sqfw_formSummary formSummary : formSummaryList) {
                int total = 0;
                JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(formSummary));
                //A21-A45的问题
                for (int i = 21; i < 46; i++) {
                    String key = "a" + i;
                    JSONArray jsonArray = jsonObject.getJSONArray(key);
                    String array_1 = jsonArray.getString(0);
                    String array_4 = jsonArray.getString(3);
                    String array_5 = jsonArray.getString(4);
                    String array_6 = jsonArray.getString(5);
                    String change = i < 45 ? getChange(formSummaryList, formSummary, key) :
                            getHiddenChange(formSummaryList, formSummary, key);
                    if ("未录入黔小消".contains(array_5)) {
                        continue;
                    }
                    if (!"是/否/无".contains(array_1.toString()) && "火灾".contains(array_4.toString()) && !"已整改".equals(array_1)) {

                        //zhangp 2023-12-08 检查数据时只要有数据就返回
                        if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                            return "true";
                        }

                        total++;
                        num++;
                        Map<String, Object> rollDataMap = Maps.newHashMap();
                        rollDataMap.put("num", num);
                        rollDataMap.put("fillDate", DateUtil.format(formSummary.getFillDate(), "yyyy年MM月dd日"));
                        rollDataMap.put("a1", formSummary.getA1());
                        rollDataMap.put("a2", formSummary.getA2());
                        rollDataMap.put("a3_a4", formSummary.getA3() + "(" + formSummary.getA4() + ")");
                        if (StrUtil.isBlank(array_5)) {
                            rollDataMap.put("hiddenProblem_5", "暂未发现隐患");
                        } else {
                            rollDataMap.put("hiddenProblem_5", array_5);
                        }
                        rollDataMap.put("hiddenProblem_6", array_6);
                        //rollDataMap.put("change", getChange(formSummaryList, formSummary, key));
                        rollDataMap.put("change", array_1.contains("现场") ? "已整改" : change);
                        rollDataMap.put("remark", null);
                        dataList.add(rollDataMap);
                    }
                }

                //A8-A12、A14的问题
                for (int i = 8; i < 15; i++) {
                    if (i != 13) {
                        String key = "a" + i;
                        JSONArray jsonArray = jsonObject.getJSONArray(key);
                        String array_1 = jsonArray.getString(0);
                        String array_4 = jsonArray.getString(3);
                        String array_5 = jsonArray.getString(4);
                        String array_6 = jsonArray.getString(5);
                        String change = i < 14 ? getChange(formSummaryList, formSummary, key) :
                                getHiddenChange(formSummaryList, formSummary, key);
                        if ("未录入黔小消".contains(array_5)) {
                            continue;
                        }
                        if (!"是/否/正常/已安装并正常使用/无".contains(array_1) && !"已整改".equals(array_1)) {

                            //zhangp 2023-12-08 检查数据时只要有数据就返回
                            if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                                return "true";
                            }

                            total++;
                            num++;
                            Map<String, Object> rollDataMap = Maps.newHashMap();
                            rollDataMap.put("num", num);
                            rollDataMap.put("fillDate", DateUtil.format(formSummary.getFillDate(), "yyyy年MM月dd日"));
                            rollDataMap.put("a1", formSummary.getA1());
                            rollDataMap.put("a2", formSummary.getA2());
                            rollDataMap.put("a3_a4", formSummary.getA3() + "(" + formSummary.getA4() + ")");
                            if (StrUtil.isBlank(array_5)) {
                                rollDataMap.put("hiddenProblem_5", "暂未发现隐患");
                            } else {
                                rollDataMap.put("hiddenProblem_5", array_5);
                            }
                            rollDataMap.put("hiddenProblem_6", array_6);
                            //rollDataMap.put("change", getChange(formSummaryList, formSummary, key));
                            rollDataMap.put("change", array_1.contains("现场") ? "已整改" : change);
                            rollDataMap.put("remark", null);
                            dataList.add(rollDataMap);
                        }
                    }
                }

                if (total == 0) {
                    num++;
                    Map<String, Object> rollDataMap = Maps.newHashMap();
                    rollDataMap.put("num", num);
                    rollDataMap.put("fillDate", DateUtil.format(formSummary.getFillDate(), "yyyy年MM月dd日"));
                    rollDataMap.put("a1", formSummary.getA1());
                    rollDataMap.put("a2", formSummary.getA2());
                    rollDataMap.put("a3_a4", formSummary.getA3() + "(" + formSummary.getA4() + ")");
                    rollDataMap.put("hiddenProblem_5", "暂未发现隐患");
                    rollDataMap.put("hiddenProblem_6", null);
                    rollDataMap.put("change", null);
                    rollDataMap.put("remark", null);
                    dataList.add(rollDataMap);
                }
                //zhangp 2023-12-08 检查数据时只要有数据就返回
                if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                    if (!dataList.isEmpty()) {
                        return "true";
                    }
                }
            }

            //zhangp  2023-12-04 检测数据时使用
            if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                if (dataList.isEmpty()) {
                    return "false";
                }
            }
            String templatePath = CommonFormOfficeUtils.getTemplatePathByOS("yuHong/fireSafetyPatrol.xlsx");
            //String fileName = "消防安全巡查台账";
            //zhangp 2023-12-13 文件名统一
            String fileName = DownLoadFileNameEnum.YT_FIRE_SAFETY_INSPECTION.getName();
            if (StringUtils.isNotBlank(excelParamDTO.getFileNameSuffix()))
                fileName += excelParamDTO.getFileNameSuffix();

            ExcelFileCreateParamBO excelFileCreateParam = new ExcelFileCreateParamBO();
            excelFileCreateParam.setTemplatePath(templatePath);
            excelFileCreateParam.setSheetParam(titleMap);
            excelFileCreateParam.setDataList(dataList);
            excelFileCreateParam.setClazz(String.class);
            excelFileCreateParam.setFileName(fileName);
            excelFileCreateParam.setReturnWebUrlFlag(excelParamDTO.getReturnWebUrlFlag());

            return CommonFormOfficeUtils.createExcelAndUpload(excelFileCreateParam);
        }

        // 安全生产检查台账
        if (fileIndex == 3) {
            titleMap.put("createBy", userName);
            if ("isTemplate-web".equals(isCheckData)) {
                titleMap.put("county_community", "云潭街道");
            } else {
                titleMap.put("county_community", county + "-" + (StrUtil.isBlank(community) ? "" : community));
            }
            titleMap.put("downloadDate", DateUtil.format(new Date(), "yyyy年MM月dd日"));

            List<Map<String, Object>> dataList = Lists.newArrayList();

            int num = 0;
            for (Sqfw_formSummary formSummary : formSummaryList) {
                int total = 0;
                JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(formSummary));
                //A21-A45的问题
                for (int i = 21; i < 46; i++) {
                    String key = "a" + i;
                    JSONArray jsonArray = jsonObject.getJSONArray(key);
                    String array_1 = jsonArray.getString(0);
                    String array_2 = jsonArray.getString(1);
                    String array_4 = jsonArray.getString(3);
                    String array_5 = jsonArray.getString(4);
                    String array_7 = jsonArray.getString(6);
                    String array_8 = jsonArray.getString(7);
                    String change = i < 45 ? getChange(formSummaryList, formSummary, key) :
                            getHiddenChange(formSummaryList, formSummary, key);
                    String picture = i < 45 ? getChangePicture(formSummaryList, formSummary, key) :
                            getHiddenPicture(formSummaryList, formSummary, key);
                    if ("未录入黔小消".contains(array_5)) {
                        continue;
                    }
                    if (!"是/否/无".contains(array_1) && "火灾".contains(array_4) && !"已整改".equals(array_1)) {

                        //zhangp 2023-12-08 检查数据时只要有数据就返回
                        if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                            return "true";
                        }

                        total++;
                        num++;
                        Map<String, Object> rollDataMap = Maps.newHashMap();
                        rollDataMap.put("num", num);
                        rollDataMap.put("fillDate", DateUtil.format(formSummary.getFillDate(), "yyyy年MM月dd日"));
                        rollDataMap.put("createBy", formSummary.getCreateUserName());
                        rollDataMap.put("a1", formSummary.getA1());
                        rollDataMap.put("a16", formSummary.getA16());
                        if (array_1.contains("现场") || array_1.contains("限期")) {
                            rollDataMap.put("hiddenProblem_1", "是");
                            if ("一般/较大".contains(array_2)) {
                                rollDataMap.put("hiddenProblem_2_1", "是");
                            } else {
                                rollDataMap.put("hiddenProblem_2_1", null);
                            }
                            if ("重大".contains(array_2)) {
                                rollDataMap.put("hiddenProblem_2_2", "是");
                            } else {
                                rollDataMap.put("hiddenProblem_2_2", null);
                            }
                        } else {
                            rollDataMap.put("hiddenProblem_1", "暂未发现隐患");
                            rollDataMap.put("hiddenProblem_2_1", null);
                            rollDataMap.put("hiddenProblem_2_2", null);
                        }
                        rollDataMap.put("hiddenProblem_5", array_5);
                        String a7 = formSummary.getA7();
                        String day = "立即";
                        if (!StrUtil.isBlank(a7)) {
                            Date date = DateUtil.parse(a7, "yyyy年MM月dd日");
                            day = getDay(formSummary.getFillDate(), date);
                        }
                        rollDataMap.put("a7", array_1.contains("现场") ? "立即" : day);
                        rollDataMap.put("a7_1", array_1.contains("现场") ? DateUtil.format(formSummary.getFillDate(), "yyyy年MM月dd日") : a7);
                        rollDataMap.put("change", array_1.contains("现场") ? "已整改" : change);
                        rollDataMap.put("a3_a4", formSummary.getA3() + "(" + formSummary.getA4() + ")");
                        //图片处理
                        List<byte[]> a18Bytes = CommonFormOfficeUtils.addPictureToByteList(formSummary.getA18(), 2);
                        rollDataMap.put("a18", a18Bytes);
                        List<byte[]> array_7Bytes = CommonFormOfficeUtils.addPictureToByteList(array_7, 1);
                        rollDataMap.put("hiddenProblem_7", array_7Bytes);

                        String url = array_1.contains("现场") ? array_8 : picture;
                        List<byte[]> array_8Bytes = CommonFormOfficeUtils.addPictureToByteList(url, 1);
                        rollDataMap.put("hiddenProblem_8", array_8Bytes);

                        rollDataMap.put("a19", formSummary.getA19());
                        rollDataMap.put("remark", null);
                        dataList.add(rollDataMap);
                    }
                }

                //A8-A12、A14的问题
                for (int i = 8; i < 15; i++) {
                    if (i != 13) {
                        String key = "a" + i;
                        JSONArray jsonArray = jsonObject.getJSONArray(key);
                        String array_1 = jsonArray.getString(0);
                        String array_2 = jsonArray.getString(1);
                        String array_4 = jsonArray.getString(3);
                        String array_5 = jsonArray.getString(4);
                        String array_7 = jsonArray.getString(6);
                        String array_8 = jsonArray.getString(7);
                        String change = i < 14 ? getChange(formSummaryList, formSummary, key) :
                                getHiddenChange(formSummaryList, formSummary, key);
                        String picture = i < 14 ? getChangePicture(formSummaryList, formSummary, key) :
                                getHiddenPicture(formSummaryList, formSummary, key);
                        if ("未录入黔小消".contains(array_5)) {
                            continue;
                        }
                        if (!"是/否/正常/已安装并正常使用/无".contains(array_1) &&
                                "火灾".contains(array_4) && !"已整改".equals(array_1)) {

                            //zhangp 2023-12-08 检查数据时只要有数据就返回
                            if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                                return "true";
                            }

                            total++;
                            num++;
                            Map<String, Object> rollDataMap = Maps.newHashMap();
                            rollDataMap.put("num", num);
                            rollDataMap.put("fillDate", DateUtil.format(formSummary.getFillDate(), "yyyy年MM月dd日"));
                            rollDataMap.put("createBy", formSummary.getCreateUserName());
                            rollDataMap.put("a1", formSummary.getA1());
                            rollDataMap.put("a16", formSummary.getA16());
                            if (array_1.contains("现场") || array_1.contains("限期")) {
                                rollDataMap.put("hiddenProblem_1", "是");
                                if ("一般/较大".contains(array_2)) {
                                    rollDataMap.put("hiddenProblem_2_1", "是");
                                } else {
                                    rollDataMap.put("hiddenProblem_2_1", null);
                                }
                                if ("重大".contains(array_2)) {
                                    rollDataMap.put("hiddenProblem_2_2", "是");
                                } else {
                                    rollDataMap.put("hiddenProblem_2_2", null);
                                }
                            } else {
                                rollDataMap.put("hiddenProblem_1", "暂未发现隐患");
                                rollDataMap.put("hiddenProblem_2_1", null);
                                rollDataMap.put("hiddenProblem_2_2", null);
                            }

                            rollDataMap.put("hiddenProblem_5", array_5);
                            String a7 = formSummary.getA7();
                            String day = "立即";
                            if (!StrUtil.isBlank(a7)) {
                                Date date = DateUtil.parse(a7, "yyyy年MM月dd日");
                                day = getDay(formSummary.getFillDate(), date);
                            }
                            rollDataMap.put("a7", array_1.contains("现场") ? "立即" : day);
                            rollDataMap.put("a7_1", array_1.contains("现场") ? DateUtil.format(formSummary.getFillDate(), "yyyy年MM月dd日") : a7);
                            rollDataMap.put("change", array_1.contains("现场") ? "已整改" : change);
                            rollDataMap.put("a3_a4", formSummary.getA3() + "(" + formSummary.getA4() + ")");
                            //图片处理
                            List<byte[]> a18Bytes = CommonFormOfficeUtils.addPictureToByteList(formSummary.getA18(), 2);
                            rollDataMap.put("a18", a18Bytes);
                            List<byte[]> array_7Bytes = CommonFormOfficeUtils.addPictureToByteList(array_7, 1);
                            rollDataMap.put("hiddenProblem_7", array_7Bytes);

                            String url = array_1.contains("现场") ? array_8 : picture;
                            List<byte[]> array_8Bytes = CommonFormOfficeUtils.addPictureToByteList(url, 1);
                            rollDataMap.put("hiddenProblem_8", array_8Bytes);

                            rollDataMap.put("a19", formSummary.getA19());
                            rollDataMap.put("remark", null);
                            dataList.add(rollDataMap);
                        }
                    }
                }

                if (total == 0) {
                    num++;
                    Map<String, Object> rollDataMap = Maps.newHashMap();
                    rollDataMap.put("num", num);
                    rollDataMap.put("fillDate", DateUtil.format(formSummary.getFillDate(), "yyyy年MM月dd日"));
                    rollDataMap.put("createBy", formSummary.getCreateUserName());
                    rollDataMap.put("a1", formSummary.getA1());
                    rollDataMap.put("a16", formSummary.getA16());
                    rollDataMap.put("hiddenProblem_1", "暂未发现隐患");
                    rollDataMap.put("hiddenProblem_2_1", null);
                    rollDataMap.put("hiddenProblem_2_2", null);
                    rollDataMap.put("hiddenProblem_5", null);
                    rollDataMap.put("a7", "");
                    rollDataMap.put("a7_1", "");
                    rollDataMap.put("change", null);
                    rollDataMap.put("a3_a4", formSummary.getA3() + "(" + formSummary.getA4() + ")");
                    //图片处理
                    List<byte[]> a18Bytes = CommonFormOfficeUtils.addPictureToByteList(formSummary.getA18(), 2);
                    rollDataMap.put("a18", a18Bytes);
                    rollDataMap.put("hiddenProblem_7", Lists.newArrayList());
                    rollDataMap.put("hiddenProblem_8", Lists.newArrayList());
                    rollDataMap.put("a19", formSummary.getA19());
                    rollDataMap.put("remark", null);
                    dataList.add(rollDataMap);
                }
                //zhangp 2023-12-08 检查数据时只要有数据就返回
                if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                    if (!dataList.isEmpty()) {
                        return "true";
                    }
                }
            }
            //zhangp  2023-12-04 检测数据时使用
            if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                if (dataList.isEmpty()) {
                    return "false";
                }
            }
            String templatePath = CommonFormOfficeUtils.getTemplatePathByOS("yuHong/workSafetyInspection.xlsx");
            //String fileName = "安全生产检查台账";
            //zhangp 2023-12-13 文件名统一
            String fileName = DownLoadFileNameEnum.YT_SAFE_PRODUCT_INSPECTION.getName();
            if (StringUtils.isNotBlank(excelParamDTO.getFileNameSuffix()))
                fileName += excelParamDTO.getFileNameSuffix();

            ExcelFileCreateParamBO excelFileCreateParam = new ExcelFileCreateParamBO();
            excelFileCreateParam.setTemplatePath(templatePath);
            excelFileCreateParam.setSheetParam(titleMap);
            excelFileCreateParam.setDataList(dataList);
            excelFileCreateParam.setClazz(FireHiddenExcelYuHongBO.class);
            excelFileCreateParam.setFileName(fileName);
            excelFileCreateParam.setReturnWebUrlFlag(excelParamDTO.getReturnWebUrlFlag());

            return CommonFormOfficeUtils.createExcelAndUpload(excelFileCreateParam);
        }

        // 瓶装液化石油气用户安全检查表
        if (fileIndex == 4) {
            titleMap.put("createBy", userName);
            titleMap.put("downloadDate", DateUtil.format(new Date(), "yyyy年MM月dd日"));

            List<Map<String, Object>> dataList = Lists.newArrayList();

            int num = 0;
            for (Sqfw_formSummary formSummary : formSummaryList) {
                String a6 = formSummary.getA6();
                //只有A6选择了液化石油气/城镇燃气用二甲醚才有该表
                if (a6.contains("液化石油气") || a6.contains("城镇燃气用二甲醚")) {

                    //zhangp 2023-12-08 检查数据时只要有数据就返回
                    if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                        return "true";
                    }

                    num++;
                    Map<String, Object> dataMap = Maps.newHashMap();
                    dataMap.put("num", num);
                    dataMap.put("a1", formSummary.getA1());
                    dataMap.put("a2", formSummary.getA2());
                    StringBuilder a6_b = new StringBuilder();
                    if (a6.contains("液化石油气")) a6_b.append("①");
                    if (a6.contains("城镇燃气用二甲醚")) a6_b.append("②");
                    dataMap.put("a6", a6_b.toString());

                    if (StringUtils.isBlank(formSummary.getA49())) {
                        dataMap.put("a49", "②");
                    } else {
                        String a49 = JSON.parseArray(formSummary.getA49()).getString(0);
                        StringBuilder a49_b = new StringBuilder();
                        if ("现场/限期".contains(a49)) {
                            a49_b.append("①");
                        } else {
                            a49_b.append("②");
                        }
                        dataMap.put("a49", a49_b.toString());
                    }

                    String a8 = JSON.parseArray(formSummary.getA8()).getString(0);
                    StringBuilder a8_b = new StringBuilder();
                    if (a8.contains("正常") || a8.contains("已整改")) a8_b.append("①");
                    if (a8.contains("现场") || a8.contains("限期")) {
                        if (a8.contains("异常")) a8_b.append("②");
                        if (a8.contains("无信息")) a8_b.append("③");
                        if (a8.contains("无芯片")) a8_b.append("③");
                        if (a8.contains("其他")) a8_b.append("③");
                    }
                    dataMap.put("a8", a8_b.toString());
                    String a9 = JSON.parseArray(formSummary.getA9()).getString(0);
                    dataMap.put("a9", (a9.contains("现场") || a9.contains("限期") ? "否" : "是"));
                    String a10 = JSON.parseArray(formSummary.getA10()).getString(0);
                    StringBuilder a10_b = new StringBuilder();
                    if (a10.contains("正常") || a10.contains("已整改")) a10_b.append("①");
                    if (a10.contains("现场") || a10.contains("限期")) {
                        if (a10.contains("长度小于")) a10_b.append("②");
                        if (a10.contains("使用三通连接")) a10_b.append("③");
                        if (a10.contains("龟")) a10_b.append("④");
                        if (a10.contains("泄漏")) a10_b.append("⑤泄漏");
                        if (a10.contains("其他")) a10_b.append("⑤");
                    }
                    dataMap.put("a10", a10_b.toString());
                    String a11 = JSON.parseArray(formSummary.getA11()).getString(0);
                    dataMap.put("a11", "否".contains(a11) ? "否" : "是");
                    String a12 = JSON.parseArray(formSummary.getA12()).getString(0);
                    StringBuilder a12_b = new StringBuilder();
                    if (a12.contains("已安装并正常使用") || a12.contains("已整改")) a12_b.append("①");
                    if (a12.contains("限期") || a12.contains("现场")) {
                        if (a12.contains("已安装未正常使用")) a12_b.append("②");
                        if (a12.contains("未安装")) a12_b.append("③");
                        if (a12.contains("其他")) a12_b.append("③");
                    }
                    dataMap.put("a12", a12_b.toString());
                    dataMap.put("a13", formSummary.getA13());

                    dataMap.put("a14", JSON.parseArray(formSummary.getA14()).getString(4));

                    dataMap.put("remark", null);
                    dataList.add(dataMap);
                }
                //zhangp 2023-12-08 检查数据时只要有数据就返回
                if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                    if (!dataList.isEmpty()) {
                        return "true";
                    }
                }
            }
            //zhangp  2023-12-04 检测数据时使用
            if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                if (dataList.isEmpty()) {
                    return "false";
                }
            }

            String templatePath = CommonFormOfficeUtils.getTemplatePathByOS("yuHong/gasSafetyManagement.xlsx");
            //String fileName = "瓶装液化石油气用户安全检查表";
            //zhangp 2023-12-13 文件名统一
            String fileName = DownLoadFileNameEnum.YT_GAS_SAFETY_MANAGE_SUPERVISE.getName();
            if (StringUtils.isNotBlank(excelParamDTO.getFileNameSuffix()))
                fileName += excelParamDTO.getFileNameSuffix();

            ExcelFileCreateParamBO excelFileCreateParam = new ExcelFileCreateParamBO();
            excelFileCreateParam.setTemplatePath(templatePath);
            excelFileCreateParam.setSheetParam(titleMap);
            excelFileCreateParam.setDataList(dataList);
            excelFileCreateParam.setClazz(String.class);
            excelFileCreateParam.setFileName(fileName);
            excelFileCreateParam.setReturnWebUrlFlag(excelParamDTO.getReturnWebUrlFlag());

            return CommonFormOfficeUtils.createExcelAndUpload(excelFileCreateParam);
        }

        // 九小场所违规住人整改台账
        if (fileIndex == 5) {
            titleMap.put("downloadDate", DateUtil.format(new Date(), "yyyy年MM月dd日"));

            List<Map<String, Object>> dataList = Lists.newArrayList();

            int num = 0;
            for (Sqfw_formSummary formSummary : formSummaryList) {
                JSONArray a26 = JSON.parseArray(formSummary.getA26());
                String a26_1 = a26.getString(0);
                String a26_8 = a26.getString(7);
                //只有A26不等于无才生成表
                if (!"无".equals(a26_1) && !"已整改".equals(a26_1)) {

                    //zhangp 2023-12-08 检查数据时只要有数据就返回
                    if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                        return "true";
                    }

                    num++;
                    Map<String, Object> rollDataMap = Maps.newHashMap();
                    rollDataMap.put("num", num);
                    rollDataMap.put("a1", formSummary.getA1());
                    rollDataMap.put("a2", formSummary.getA2());
                    rollDataMap.put("a3_a4", formSummary.getA3() + "(" + formSummary.getA4() + ")");
                    //图片处理
                    List<byte[]> a18Bytes = CommonFormOfficeUtils.addPictureToByteList(formSummary.getA18(), 1);
                    rollDataMap.put("a18", a18Bytes);
                    List<byte[]> array_7Bytes = CommonFormOfficeUtils.addPictureToByteList(a26.getString(6), 1);
                    rollDataMap.put("a26_7", array_7Bytes);
                    String url = "现场".equals(a26_1) ? a26_8 : getChangePicture(formSummaryList, formSummary, "a26");
                    List<byte[]> array_8Bytes = CommonFormOfficeUtils.addPictureToByteList(url, 1);
                    rollDataMap.put("a26_8", array_8Bytes);
                    if (url == null && !"已整改".equals(a26_1)) rollDataMap.put("a26_8_1", "未整改");
                    rollDataMap.put("fillDate", DateUtil.format(formSummary.getFillDate(), "yyyy年MM月dd日"));
                    dataList.add(rollDataMap);
                }
                //zhangp 2023-12-08 检查数据时只要有数据就返回
                if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                    if (!dataList.isEmpty()) {
                        return "true";
                    }
                }
            }

            // wzy 2023-11-15 可以下载空模板
            // if (num == 0) return null;

            //zhangp  2023-12-04 检测数据时使用
            if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                if (dataList.isEmpty()) {
                    return "false";
                }
            }
            String templatePath = CommonFormOfficeUtils.getTemplatePathByOS("yuHong/nineSmallPremisesIllegalResidence.xlsx");
            //String fileName = "九小场所违规住人整改台账";
            //zhangp 2023-12-13 文件名统一
            String fileName = DownLoadFileNameEnum.YT_NINE_SMALL_ILLEGAL_RESIDENTS.getName();
            if (StringUtils.isNotBlank(excelParamDTO.getFileNameSuffix()))
                fileName += excelParamDTO.getFileNameSuffix();

            ExcelFileCreateParamBO excelFileCreateParam = new ExcelFileCreateParamBO();
            excelFileCreateParam.setTemplatePath(templatePath);
            excelFileCreateParam.setSheetParam(titleMap);
            excelFileCreateParam.setDataList(dataList);
            excelFileCreateParam.setClazz(FireHiddenExcelYuHongBO.class);
            excelFileCreateParam.setFileName(fileName);
            excelFileCreateParam.setReturnWebUrlFlag(excelParamDTO.getReturnWebUrlFlag());

            return CommonFormOfficeUtils.createExcelAndUpload(excelFileCreateParam);
        }

        // 欠薪风险排查工作表
        if (fileIndex == 6) {
            titleMap.put("downloadDate", DateUtil.format(new Date(), "yyyy年MM月dd日"));
            if ("isTemplate-web".equals(isCheckData)) {
                titleMap.put("county", "云潭街道");
                titleMap.put("community", "");

            }
            List<Map<String, Object>> dataList = Lists.newArrayList();

            int num = 0;
            for (Sqfw_formSummary formSummary : formSummaryList) {
                String str = formSummary.getA46();
                if (StrUtil.isBlank(str)) continue;
                JSONArray a46 = JSON.parseArray(str);

                //zhangp 2023-12-08 检查数据时只要有数据就返回
                if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                    return "true";
                }

                num++;
                Map<String, Object> rollDataMap = Maps.newHashMap();
                rollDataMap.put("num", num);
                rollDataMap.put("a1", formSummary.getA1());
                rollDataMap.put("a2", formSummary.getA2());
                rollDataMap.put("a3", formSummary.getA3());
                rollDataMap.put("a4", formSummary.getA4());
                rollDataMap.put("a17", formSummary.getA17());
                rollDataMap.put("a19", formSummary.getA19());
                rollDataMap.put("a46_1", "家人".equals(a46.get(0)) ? a46.get(0) : "员工" + a46.get(0) + "人");
                rollDataMap.put("a46_2", a46.get(1));
                rollDataMap.put("a46_3", a46.get(2));
                rollDataMap.put("fillDate", DateUtil.format(formSummary.getFillDate(), "yyyy年MM月dd日"));
                dataList.add(rollDataMap);
                //zhangp 2023-12-08 检查数据时只要有数据就返回
                if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                    return "true";
                }
            }
            //zhangp  2023-12-04 检测数据时使用
            if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                if (dataList.isEmpty()) {
                    return "false";
                }
            }

            String templatePath = CommonFormOfficeUtils.getTemplatePathByOS("yuHong/backPayRiskScreening.xlsx");
            //String fileName = "欠薪风险排查工作表";
            //zhangp 2023-12-13 文件名统一
            String fileName = DownLoadFileNameEnum.YT_ARREARS_RIS_INSPECTION.getName();
            if (StringUtils.isNotBlank(excelParamDTO.getFileNameSuffix()))
                fileName += excelParamDTO.getFileNameSuffix();

            ExcelFileCreateParamBO excelFileCreateParam = new ExcelFileCreateParamBO();
            excelFileCreateParam.setTemplatePath(templatePath);
            excelFileCreateParam.setSheetParam(titleMap);
            excelFileCreateParam.setDataList(dataList);
            excelFileCreateParam.setClazz(String.class);
            excelFileCreateParam.setFileName(fileName);
            excelFileCreateParam.setReturnWebUrlFlag(excelParamDTO.getReturnWebUrlFlag());

            return CommonFormOfficeUtils.createExcelAndUpload(excelFileCreateParam);
        }

        // “九小场所”隐患排查统计表
        if (fileIndex == 8) {
            List<Map<String, Object>> dataList = Lists.newArrayList();

            int num = 0;
            for (Sqfw_formSummary formSummary : formSummaryList) {
                JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(formSummary));
                //A21-A45的问题
                for (int i = 21; i < 46; i++) {
                    String key = "a" + i;
                    JSONArray jsonArray = jsonObject.getJSONArray(key);
                    String array_1 = jsonArray.getString(0);
                    String array_2 = jsonArray.getString(1);
                    String array_3 = jsonArray.getString(2);
                    String array_4 = jsonArray.getString(3);
                    String array_5 = jsonArray.getString(4);
                    String array_6 = jsonArray.getString(5);
                    String change = i < 45 ? getChange(formSummaryList, formSummary, key) :
                            getHiddenChange(formSummaryList, formSummary, key);
                    if ("未录入黔小消".contains(array_5)) {
                        continue;
                    }
                    if (!"是/否/无".contains(array_1) && !"已整改".equals(array_1)) {

                        //zhangp 2023-12-08 检查数据时只要有数据就返回
                        if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                            return "true";
                        }

                        num++;
                        Map<String, Object> rollDataMap = Maps.newHashMap();
                        rollDataMap.put("num", num);
                        rollDataMap.put("county", formSummary.getCounty());
                        rollDataMap.put("a1", formSummary.getA1());
                        rollDataMap.put("a2", formSummary.getA2());
                        rollDataMap.put("a5", formSummary.getA5());
                        rollDataMap.put("type", array_4);
                        rollDataMap.put("describe", array_5);
                        rollDataMap.put("option", array_6 + "：" +(array_1.contains("现场") ? "已整改" : change));
                        rollDataMap.put("remark", "");
                        dataList.add(rollDataMap);
                    }
                }

                //A8-A12、A14的问题
                for (int i = 8; i < 15; i++) {
                    if (i != 13) {
                        String key = "a" + i;
                        JSONArray jsonArray = jsonObject.getJSONArray(key);
                        String array_1 = jsonArray.getString(0);
                        String array_2 = jsonArray.getString(1);
                        String array_3 = jsonArray.getString(2);
                        String array_4 = jsonArray.getString(3);
                        String array_5 = jsonArray.getString(4);
                        String array_6 = jsonArray.getString(5);
                        String change = i < 14 ? getChange(formSummaryList, formSummary, key) :
                                getHiddenChange(formSummaryList, formSummary, key);
                        if ("未录入黔小消".contains(array_5)) {
                            continue;
                        }
                        if (!"是/否/正常/已安装并正常使用/无".contains(array_1) && !"已整改".equals(array_1)) {

                            //zhangp 2023-12-08 检查数据时只要有数据就返回
                            if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                                return "true";
                            }

                            num++;
                            Map<String, Object> rollDataMap = Maps.newHashMap();
                            rollDataMap.put("num", num);
                            rollDataMap.put("county", formSummary.getCounty());
                            rollDataMap.put("a1", formSummary.getA1());
                            rollDataMap.put("a2", formSummary.getA2());
                            rollDataMap.put("a5", formSummary.getA5());
                            rollDataMap.put("type", array_4);
                            rollDataMap.put("describe", array_5);
                            rollDataMap.put("option", array_6 + "：" +(array_1.contains("现场") ? "已整改" : change));
                            rollDataMap.put("remark", "");
                            dataList.add(rollDataMap);
                        }
                    }
                }
            }
            //zhangp  2023-12-04 检测数据时使用
            if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                if (dataList.isEmpty()) {
                    return "false";
                }
            }

            String templatePath = CommonFormOfficeUtils.getTemplatePathByOS("yuHong/nineHiddenDangers.xlsx");
            //zhangp 2023-12-13 文件名统一
            String fileName = DownLoadFileNameEnum.YT_NINE_HIDDEN.getName();
            if (StringUtils.isNotBlank(excelParamDTO.getFileNameSuffix()))
                fileName += excelParamDTO.getFileNameSuffix();

            ExcelFileCreateParamBO excelFileCreateParam = new ExcelFileCreateParamBO();
            excelFileCreateParam.setTemplatePath(templatePath);
            excelFileCreateParam.setSheetParam(titleMap);
            excelFileCreateParam.setDataList(dataList);
            excelFileCreateParam.setClazz(String.class);
            excelFileCreateParam.setFileName(fileName);
            excelFileCreateParam.setReturnWebUrlFlag(excelParamDTO.getReturnWebUrlFlag());

            return CommonFormOfficeUtils.createExcelAndUpload(excelFileCreateParam);
        }

        return null;
    }

    /**
     * 获取整改情况
     *
     * @param formSummaryList
     * @param formSummary
     * @return
     */
    private static String getChange(List<Sqfw_formSummary> formSummaryList, Sqfw_formSummary formSummary, String key) {
        List<Sqfw_formSummary> formSummaryListUp = new ArrayList<>();
        for (Sqfw_formSummary f : formSummaryList) {
            //zhangp 2024-01-05 A1判空,否则报空指针
            if (StringUtils.isNotBlank(f.getA1()) && f.getA1().equals(formSummary.getA1()) && f.getFillDate().getTime() > formSummary.getFillDate().getTime()) {
                formSummaryListUp.add(f);
            }
        }
        if (formSummaryListUp.size() > 0) {
            Collections.sort(formSummaryListUp, Comparator.comparing(obj -> obj.getFillDate()));//升序
            //Collections.reverse(formSummaryListUp);//反向排序
            Sqfw_formSummary f = formSummaryListUp.get(0);//获取第一个元素
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(f));
            JSONArray jsonArray = jsonObject.getJSONArray(key);
            String str = jsonArray.getString(0);
            if (str.contains("限期")) return "未整改";
            if (str.contains("已整改") || str.contains("现场")) return "已整改";
            if (!"是/否/无/正常/已安装并正常使用".contains(str)) return null;
        } else {
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(formSummary));
            JSONArray jsonArray = jsonObject.getJSONArray(key);
            String str = jsonArray.getString(0);
            if (str.contains("限期")) return "未整改";
            if (str.contains("已整改") || str.contains("现场")) return "已整改";
            if (!"是/否/无/正常/已安装并正常使用".contains(str)) return null;
        }
        return null;
    }

    private static String getHiddenChange(List<Sqfw_formSummary> formSummaryList, Sqfw_formSummary formSummary, String key) {
        List<Sqfw_formSummary> formSummaryListUp = new ArrayList<>();
        for (Sqfw_formSummary f : formSummaryList) {
            //zhangp 2024-01-05 A1判空,否则报空指针
            if (StringUtils.isNotBlank(f.getA1()) && f.getA1().equals(formSummary.getA1())) {
                formSummaryListUp.add(f);
            }
        }
        if (formSummaryListUp.size() > 1) {
            Collections.sort(formSummaryListUp, Comparator.comparing(obj -> obj.getFillDate()));//升序
            int i = -1;
            for (Sqfw_formSummary sqfwFormSummary : formSummaryListUp) {
                i++;
                if (sqfwFormSummary.getFillDate().equals(formSummary.getFillDate())) break;
            }
            Sqfw_formSummary lastFormSummary = formSummaryListUp.get(formSummaryListUp.size() - 1);
            JSONArray array = JSON.parseArray(key.contains("a45") ? lastFormSummary.getA47() : lastFormSummary.getA48());
            if (array == null) return null;
            JSONArray hidden = array.getJSONArray(i);
            String picture = hidden.getString(7);
            return StrUtil.isBlank(picture) ? "未整改" : "已整改";
        } else {
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(formSummary));
            JSONArray jsonArray = jsonObject.getJSONArray(key);
            String str = jsonArray.getString(0);
            if (str.contains("限期")) return "未整改";
            if (str.contains("已整改") || str.contains("现场")) return "已整改";
            if (!"是/否/无/正常/已安装并正常使用".contains(str)) return null;
        }
        return null;
    }

    private static String getChangePicture(List<Sqfw_formSummary> formSummaryList, Sqfw_formSummary formSummary, String key) {
        List<Sqfw_formSummary> formSummaryListUp = new ArrayList<>();
        for (Sqfw_formSummary f : formSummaryList) {
            //zhangp 2024-01-05 A1判断，否则报空
            if (StringUtils.isNotBlank(f.getA1()) && f.getA1().equals(formSummary.getA1()) && f.getFillDate().getTime() > formSummary.getFillDate().getTime()) {
                formSummaryListUp.add(f);
            }
        }
        if (formSummaryListUp.size() > 0) {
            Collections.sort(formSummaryListUp, Comparator.comparing(obj -> obj.getFillDate()));//升序
            //Collections.reverse(formSummaryListUp);//反向排序
            Sqfw_formSummary f = formSummaryListUp.get(0);//获取第一个元素
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(f));
            JSONArray jsonArray = jsonObject.getJSONArray(key);
            String str = jsonArray.getString(7);
            return str;
        } else {
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(formSummary));
            JSONArray jsonArray = jsonObject.getJSONArray(key);
            String str = jsonArray.getString(7);
            return str;
        }
    }

    private static String getHiddenPicture(List<Sqfw_formSummary> formSummaryList, Sqfw_formSummary formSummary, String key) {
        List<Sqfw_formSummary> formSummaryListUp = new ArrayList<>();
        for (Sqfw_formSummary f : formSummaryList) {
            //zhangp 2024-01-05 A1判断，否则报空
            if (StringUtils.isNotBlank(f.getA1()) && f.getA1().equals(formSummary.getA1())) {
                formSummaryListUp.add(f);
            }
        }
        if (formSummaryListUp.size() > 1) {
            Collections.sort(formSummaryListUp, Comparator.comparing(obj -> obj.getFillDate()));//升序
            int i = -1;
            for (Sqfw_formSummary sqfwFormSummary : formSummaryListUp) {
                i++;
                if (sqfwFormSummary.getFillDate().equals(formSummary.getFillDate())) break;
            }
            Sqfw_formSummary lastFormSummary = formSummaryListUp.get(formSummaryListUp.size() - 1);
            JSONArray array = JSON.parseArray(key.contains("a45") ? lastFormSummary.getA47() : lastFormSummary.getA48());
            if (array == null) return null;
            JSONArray hidden = array.getJSONArray(i);
            String picture = hidden.getString(7);
            return StrUtil.isBlank(picture) ? null : picture;
        } else {
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(formSummary));
            JSONArray jsonArray = jsonObject.getJSONArray(key);
            String str = jsonArray.getString(7);
            return str;
        }
    }

    private static String getDay(Date startDate, Date endDate) {
        Date date = startDate;
        long i = 0;
        long betweenDay = DateUtil.between(startDate, endDate, DateUnit.DAY);
        long n = betweenDay;
        while (n > 0) {
            n = DateUtil.between(date, endDate, DateUnit.DAY);
            date = DateUtil.offsetDay(date, 1);
            Instant instant = date.toInstant();
            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            DayOfWeek week = localDateTime.getDayOfWeek();
            if (week == DayOfWeek.SATURDAY || week == DayOfWeek.SUNDAY) {
                i++;
            }
        }
        return betweenDay - i + 1 + "个工作日";
    }
}
